package com.ctshk.rpc.order.train.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.service.mapper.CtsBaseMapper;
import com.ctshk.rpc.order.train.dto.TrainOrderPageDTO;
import com.ctshk.rpc.order.train.entity.TrainOrder;
import com.ctshk.rpc.order.train.req.TrainOrderPageReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 火车票订单表 Mapper 接口
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-17
 */
public interface TrainOrderMapper extends CtsBaseMapper<TrainOrder> {


    /**
     * 获取巴士路线列表
     * @param req
     * @return
     */
    @Select("<script>"+"SELECT o.id,o.order_number,o.product_type,o.railway_corporation_name,o.train_number_name, \n" +
            "o.departure_city_name,o.arrive_city_name,o.boarding_station_name,o.seat_level_name, \n" +
            "o.alighting_station_name,o.order_amount,o.payment_status,o.scheduled_status, \n" +
            "o.contact_name,o.source,o.salesman_name,o.gmt_create,o.pay_expire_date, create_id \n" +
            "FROM t_train_order o \n" +
            "WHERE o.is_deleted = 0 <if test=\"req.orderNumber != null and req.orderNumber != ''\">AND o.order_number = #{req.orderNumber}</if> \n" +
            "<if test=\"req.productType != null and req.productType != 0\">AND o.product_type = #{req.productType}</if> \n" +
            "<if test=\"req.railwayCorporationName != null and req.railwayCorporationName != ''\">AND o.railway_corporation_name LIKE CONCAT('%',#{req.railwayCorporationName},'%')</if> \n" +
            "<if test=\"req.trainNumberName != null and req.trainNumberName != ''\">AND o.train_number_name LIKE CONCAT('%',#{req.trainNumberName},'%')</if> \n" +
            "<if test=\"req.departureCityName != null and req.departureCityName != ''\">AND o.departure_city_name LIKE CONCAT('%',#{req.departureCityName},'%')</if> \n" +
            "<if test=\"req.arriveCityName != null and req.arriveCityName != ''\">AND o.arrive_city_name LIKE CONCAT('%',#{req.arriveCityName},'%')</if> \n" +
            "<if test=\"req.boardingStationName != null and req.boardingStationName != ''\">AND o.boarding_station_name LIKE CONCAT('%',#{req.boardingStationName},'%')</if> \n" +
            "<if test=\"req.alightingStationName != null and req.alightingStationName != ''\">AND o.alighting_station_name LIKE CONCAT('%',#{req.alightingStationName},'%')</if> \n" +
            "<if test=\"req.scheduledStatus != null and req.scheduledStatus != 0\">AND o.scheduled_status = #{req.scheduledStatus}</if> \n" +
            "<if test=\"req.paymentStatus != null and req.paymentStatus != 0\">AND o.payment_status = #{req.paymentStatus}</if> \n" +
            "<if test=\"req.beginDate != null and req.endDate != null\">AND DATE_FORMAT(o.gmt_create,'%Y-%m-%d') between #{req.beginDate} and #{req.endDate}</if> \n" +
            "<if test=\"req.contactName != null and req.contactName != ''\">AND o.contact_name =#{req.contactName}</if> \n" +
            "<if test=\"req.source != null and req.source != 0\">AND o.source = #{req.source}</if> \n" +
            "<if test=\"req.dataPermissionUserIds != null\">" +
            "and o.salesman_id in " +
            "<foreach collection='req.dataPermissionUserIds' item='id' open='(' separator=',' close=')'>"+
            "#{id}"+
            "</foreach></if> \n"+
            "<if test=\"req.salesmanName != null and req.salesmanName != ''\">AND o.salesman_name =#{req.salesmanName}</if> \n" +
            "order by  o.gmt_create desc "+"</script>")
    IPage<TrainOrderPageDTO> queryList(@Param("req") TrainOrderPageReq req, IPage<TrainOrderPageDTO> agentPage);

    @Select({
            "SELECT count(train_id) FROM t_train_order ",
            "where is_deleted = 0 ",
            "and train_id = #{id} ",
            "and (payment_status = 2 or scheduled_status = 1)  ",
            "and  DATEDIFF(NOW(),gmt_create) BETWEEN 0 AND 7  "
    })
    Integer selectSaleCount(@Param("id") Long id);
}
